﻿using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WMS.ClientService.Dto;
using WMS.ClientService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
using System.Collections.Generic;
using System.IO;
using System;
using WMS.BinLocationDefinitionService;
using WMS.TakenoffService;
using WMS.TakenoffService.Dto;
using WMS.BinLocationDefinitionService.Dto;

namespace WMS.Web.Controllers
{
    public class PuffController : Controller
    {
        private readonly IClientServices clientServices;
        private readonly IBinLocationDefinitionService binLocationDefinitionService;
        private readonly ITakenoffService teakenoffService;

        public PuffController(IClientServices clientServices, IBinLocationDefinitionService binLocationDefinitionService, ITakenoffService teakenoffService)
        {
            this.clientServices = clientServices;
            this.binLocationDefinitionService = binLocationDefinitionService;
            this.teakenoffService = teakenoffService;
        }



        //---------------------------------------客户***************************************************
        public IActionResult ClientList()
        {
            return View();
        }

        public IActionResult AddClientView()
        {
            return View();
        }

        public IActionResult EditClientView(int id)
        {
            ViewBag.Id = id;
            return View();
        }

        /// <summary>
        /// 获取客户数据
        /// </summary>
        /// <param name="chineseName"></param>
        /// <param name="contractNumber"></param>
        /// <param name="code"></param>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<JsonResult> GetCilentList(string? chineseName, string? contractNumber, string? code, int index = 1, int limit = 10)
        {
            var clientlist = await clientServices.GetCilentsdata(chineseName, contractNumber, code, index, limit);
            return Json(new { data = clientlist.Queryable, count = clientlist.RowCount, code = 0, msg = "" });
        }

        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpfilePost(IFormFile file)
        {
            var str=Guid.NewGuid().ToString();
            var savepath = Path.Combine("upload", str + file.FileName);
            using (var stream = new FileStream(savepath, FileMode.Create))
            {
                file.CopyToAsync(stream);
            }
            return Ok(new { filename = "/upload/" + str + file.FileName, size = file.Length });
        }


        /// <summary>
        /// 添加客户数据
        /// </summary>
        /// <param name="addclientdto"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> AddCilentdatas(Addclientdto addclientdto)
        {
            var clientdto = await clientServices.AddClients(addclientdto);
            return clientdto;
        }

        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> DeleClients(int id)
        {
            var clientdto = await clientServices.DeleteCilents(id);
            return clientdto;
        }

        /// <summary>
        /// 通过id获取客户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<JsonResult> GetByClients(int id)
        {
            var clientdto=await clientServices.SelectByidClient(id);
            return Json(new {code=0,msg="",data=clientdto});
        }

        /// <summary>
        /// 修改客户信息
        /// </summary>
        /// <param name="searchclientdto"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> UpdateClientdatas(SearchClientdto searchclientdto)
        {
            var clientdto=await clientServices.UpdateClients(searchclientdto);
            return clientdto;
        }



        //***********************************************库位定义------------------------------------------
        public IActionResult BinLocationList()
        {
            return View();
        }

        public IActionResult AddBinlocationView()
        {
            return View();
        }

        public IActionResult EditBinlocationView(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 获取库位定义数据
        /// </summary>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<JsonResult> GetBinlocaList( int index = 1, int limit = 10)
        {
            var binlotlist = await binLocationDefinitionService.GetBinlocationdata(index, limit);
            return Json(new { data = binlotlist.Queryable, count = binlotlist.RowCount, code = 0, msg = "" });
        }

        /// <summary>
        /// 删除库位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> DeleBinlocas(int id)
        {
            var bindto=await binLocationDefinitionService.DeleteBins(id);
            return bindto;
        }

        /// <summary>
        /// 添加库位信息
        /// </summary>
        /// <param name="addBinlocationdto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddBinlocationdatas(AddBinlocationdto addBinlocationdto)
        {
            var bindto=await binLocationDefinitionService.AddTBinlocas(addBinlocationdto);
            return bindto;
        }

        /// <summary>
        /// 通过id获取库位信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<JsonResult> GetByBins(int id)
        {
            var bindto = await binLocationDefinitionService.SelectByidBins(id);
            return Json(new { code = 0, msg = "", data = bindto });
        }

        /// <summary>
        /// 修改库位信息
        /// </summary>
        /// <param name="searchBinlocationdto"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> UpdateBindatas(SearchBinlocationdto searchBinlocationdto)
        {
            var bindto = await binLocationDefinitionService.UpdateClients(searchBinlocationdto);
            return bindto;
        }


        //---**********************************下架****************************************************
        public IActionResult TakenoffList()
        {
            return View();
        }

        public IActionResult AddTakenView()
        {
            return View();
        }

        public IActionResult EditTakenView(int id)
        {
            ViewBag.id = id;
            return View();
        }

        /// <summary>
        /// 获取下架数据
        /// </summary>
        /// <param name="productCode"></param>
        /// <param name="productName"></param>
        /// <param name="index"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetTakenList(string? productCode,string? productName, int index = 1, int limit = 10)
        {
            var takenlist=await teakenoffService.GetTakensdata(productCode, productName, index,limit);
            return Json(new { data = takenlist.Queryable, count = takenlist.RowCount, code = 0, msg = "" });
        }

        /// <summary>
        /// 删除下架信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> DeleTakenoffs(int id)
        {
            var takendto = await teakenoffService.DeleteTakens(id);
            return takendto;
        }

        /// <summary>
        /// 添加下架信息
        /// </summary>
        /// <param name="addTakenoffdto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddTakendatas(AddTakenoffdto addTakenoffdto)
        {
            var takendto = await teakenoffService.AddTakenoffs(addTakenoffdto);
            return takendto;
        }

        /// <summary>
        /// 通过id获取下架信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        public async Task<JsonResult> GetByTakens(int id)
        {
            var takendto = await teakenoffService.SelectByidTaken(id);
            return Json(new { code = 0, msg = "", data = takendto });
        }

        /// <summary>
        /// 修改下架信息
        /// </summary>
        /// <param name="searchclientdto"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<int> UpdateTakendatas(SearchTakendto searchTakendto)
        {
            var takendto = await teakenoffService.UpdateTakens(searchTakendto);
            return takendto;
        }
    }
}
